﻿/**************************************************************************
创建时间:	2020/5/16 23:41:16    
作	  者:	张存
邮 	  箱:	zhangcunliang@126.com

Copyright (c) zhcun.cn

描	述：
记	录：
***************************************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZhCun.DBModel;
using ZhCun.Utils;
using ZhCun.Win.ExtendSearch;

namespace ZhCun.Win.ExtendGridCombox
{
    public partial class FrmGridCombox : FrmBase
    {
        public FrmGridCombox()
        {
            InitializeComponent();
        }

        GridComboxBLL BLLObj = new GridComboxBLL();

        //当前选中的主记录对象
        Sys_GridCombox _CurrMain;

        void BindMainData()
        {
            var rData = BLLObj.GetMainData(1, ucPage.OnePageCount, tsTxtSearch.Text, out int rowCount);
            dgvMain.DataSource = rData;
            ucPage.InitiControl(rowCount);
            _CurrMain = null;
            BindColumnData();
        }

        void InitControl()
        {
            ucPage.PageChangedEvent += UcPage_PageChangedEvent;
            dgvMain.SelectionChangedAndCellClick += DgvMain_SelectionChanged;
        }

        private void DgvMain_SelectionChanged(object sender, DataGridViewCellEventArgs e)
        {
            _CurrMain = dgvMain.GetSelectedClassData<Sys_GridCombox>();
            BindColumnData();
        }

        private void UcPage_PageChangedEvent(int pageNo, int onePageCount, out int recordCount)
        {
            var rData = BLLObj.GetMainData(pageNo, onePageCount, tsTxtSearch.Text, out recordCount);
            dgvMain.DataSource = rData;
        }

        private void FrmGridCombox_Load(object sender, EventArgs e)
        {
            InitControl();

            if (Owner is IFormContainer dgvContainer)
            {
                BLLObj.FixedFormName = dgvContainer.ContainerName;

                BindMainData();
            }
        }

        private void tsBtnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void tsBtnSearch_Click(object sender, EventArgs e)
        {
            BLLObj.ClearADSql();
            BindMainData();
        }

        private void tsTxtSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                tsBtnSearch_Click(sender, e);
            }
        }

        private void tsAdSearch_Click(object sender, EventArgs e)
        {
            ADSearch(BLLObj.ADJoinSql, BindMainData, dgvMain);
        }

        FrmGridComboxEdit CreateEditForm()
        {
            FrmGridComboxEdit frm = new FrmGridComboxEdit();
            frm.FixedFormName = BLLObj.FixedFormName;
            return frm;
        }

        private void tsBtnAdd_Click(object sender, EventArgs e)
        {
            FrmGridComboxEdit frm = CreateEditForm();
            frm.FixedFormName = BLLObj.FixedFormName;
            var arg = new EditFormArgs
            {
                ClearId = true,
                SaveHandle = (m) => BLLObj.AddMain(m as Sys_GridCombox),
                SaveModel = new Sys_GridCombox()
            };
            var ret = frm.ShowDialog(arg);
            if (ret)
            {
                BindMainData();
                ShowMessage(ret.msg);
            }
        }

        private void tsBtnCopyAdd_Click(object sender, EventArgs e)
        {
            var model = dgvMain.GetSelectedClassData<Sys_GridCombox>();
            if (model == null)
            {
                ShowMessage("没有选中记录");
                return;
            }
            FrmGridComboxEdit frm = CreateEditForm();
            frm.FixedFormName = BLLObj.FixedFormName;
            var arg = new EditFormArgs
            {
                ClearId = true,
                SaveHandle = (m) => BLLObj.AddMain(m as Sys_GridCombox),
                SaveModel = new Sys_GridCombox(),
                SetModel = model
            };
            var ret = frm.ShowDialog(arg);
            if (ret)
            {
                BindMainData();
                ShowMessage(ret.msg);
            }
        }

        private void tsBtnEdit_Click(object sender, EventArgs e)
        {
            var model = dgvMain.GetSelectedClassData<Sys_GridCombox>();
            if (model == null)
            {
                ShowMessage("没有选中记录");
                return;
            }
            FrmGridComboxEdit frm = CreateEditForm();
            frm.FixedFormName = BLLObj.FixedFormName;
            var arg = new EditFormArgs
            {
                SaveHandle = (m) => BLLObj.EditMain(m as Sys_GridCombox),
                SaveModel = model,
                SetModel = model
            };
            var ret = frm.ShowDialog(arg);
            if (ret)
            {
                BindMainData();
                ShowMessage(ret.msg);
            }
        }

        private void tsBtnDelete_Click(object sender, EventArgs e)
        {
            if (!ShowQuestion("确定要删除吗？", "删除确认"))
            {
                return;
            }
            var model = dgvMain.GetSelectedClassData<Sys_GridCombox>();
            if (model == null)
            {
                ShowMessage("没有选中记录");
                return;
            }
            var ret = BLLObj.DelMain(model);
            ShowMessage(ret.msg);
            if (ret)
            {
                BindMainData();
            }
        }

        void BindColumnData()
        {
            if (_CurrMain == null)
            {
                dgvColumn.DataSource = null;
                return;
            }
            var ret = BLLObj.GetColumnData(_CurrMain.Id);
            dgvColumn.DataSource = ret;
        }

        private void tsBtnSearchColumn_Click(object sender, EventArgs e)
        {
            BindColumnData();
        }

        void AddColumn(bool isCopy)
        {
            if (_CurrMain == null)
            {
                ShowMessage("未选中绑定信息");
                return;
            }

            var editArg = new EditFormArgs
            {
                ClearId = true,
                SaveHandle = (m) => BLLObj.AddColumn(m as Sys_GridCombox_Column),
                SaveModel = new Sys_GridCombox_Column { MainId = _CurrMain.Id }
            };

            if (isCopy)
            {
                editArg.SetModel = dgvColumn.GetSelectedClassData<Sys_GridCombox_Column>();
                if (editArg.SetModel == null)
                {
                    ShowMessage("没有选中记录");
                    return;
                }
            }

            FrmGridComboxColumnEdit frm = new FrmGridComboxColumnEdit();
            frm.DgvRowCount = dgvColumn.RowCount;
            var ret = frm.ShowDialog(editArg);
            if (ret)
            {
                BindColumnData();
                ShowMessage(ret.msg);
            }
        }

        private void tsBtnAddColumn_Click(object sender, EventArgs e)
        {
            AddColumn(false);

            //if (_CurrMain == null)
            //{
            //    ShowMessage("未选中绑定信息");
            //    return;
            //}
            //FrmGridComboxColumnEdit frm = new FrmGridComboxColumnEdit();
            //frm.DgvRowCount = dgvColumn.RowCount;
            //var saveModel = new Sys_GridCombox_Column
            //{
            //    MainId = _CurrMain.Id
            //};
            //var arg = new EditFormArgs
            //{
            //    ClearId = true,
            //    SaveHandle = (m) => BLLObj.AddColumn(m as Sys_GridCombox_Column),
            //    SaveModel = saveModel
            //};
            //var ret = frm.ShowDialog(arg);
            //if (ret)
            //{
            //    BindColumnData();
            //    ShowMessage(ret.msg);
            //}
        }

        private void tsBtnCopyAddColumn_Click(object sender, EventArgs e)
        {
            AddColumn(true);
        }

        private void tsBtnEditColumn_Click(object sender, EventArgs e)
        {
            var model = dgvColumn.GetSelectedClassData<Sys_GridCombox_Column>();
            if (model == null)
            {
                ShowMessage("没有选中记录");
                return;
            }
            var arg = new EditFormArgs
            {
                SaveHandle = (m) => BLLObj.EditColumn(m as Sys_GridCombox_Column),
                SaveModel = model,
                SetModel = model
            };
            FrmGridComboxColumnEdit frm = new FrmGridComboxColumnEdit();
            var ret = frm.ShowDialog(arg);
            if (ret)
            {
                BindColumnData();
                ShowMessage(ret.msg);
            }
        }

        private void tsBtnDeleteColumn_Click(object sender, EventArgs e)
        {
            if (!ShowQuestion("确定要删除吗？", "删除确认"))
            {
                return;
            }
            var model = dgvColumn.GetSelectedClassData<Sys_GridCombox_Column>();
            if (model == null)
            {
                ShowMessage("没有选中记录");
                return;
            }
            var ret = BLLObj.DelColumn(model);
            ShowMessage(ret.msg);
            if (ret)
            {
                BindColumnData();
            }
        }
    }
}